package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.dto.JupiterUserRoleDTO;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-roles")
public class JupiterUserRoleController {

	@Autowired
	private JupiterUserRoleService userRoleService;

	@GetMapping("/{uuid}")
	public JupiterResult<JupiterUserRoleDTO> getById(@PathVariable("uuid") String uuid) {
		JupiterUserRoleDTO userRole = userRoleService.getById(uuid);
		return JupiterResult.success(userRole);
	}

	@GetMapping
	public JupiterResult<List<JupiterUserRoleDTO>> getAll() {
		List<JupiterUserRoleDTO> userRoles = userRoleService.getAll();
		return JupiterResult.success(userRoles);
	}

	@GetMapping("/user/{userId}")
	public JupiterResult<List<String>> getByUserId(@PathVariable("userId") String userId) {
		List<String> userRoles = userRoleService.getByUserId(userId);
		return JupiterResult.success(userRoles);
	}

	@GetMapping("/role/{roleId}")
	public JupiterResult<List<JupiterUserRoleDTO>> getByRoleId(@PathVariable("roleId") String roleId) {
		List<JupiterUserRoleDTO> userRoles = userRoleService.getByRoleId(roleId);
		return JupiterResult.success(userRoles);
	}

	@PostMapping
	public JupiterResult<Void> create(@RequestBody JupiterUserRoleDTO userRole) {
		userRoleService.create(userRole);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}")
	public JupiterResult<Void> update(@PathVariable("uuid") String uuid, @RequestBody JupiterUserRoleDTO userRole) {
		userRole.setUuid(uuid);
		userRoleService.update(userRole);
		return JupiterResult.success();
	}

	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> delete(@PathVariable("uuid") String uuid) {
		userRoleService.delete(uuid);
		return JupiterResult.success();
	}

	@DeleteMapping("/user/{userId}")
	public JupiterResult<Void> deleteByUserId(@PathVariable("userId") String userId) {
		userRoleService.deleteByUserId(userId);
		return JupiterResult.success();
	}

	@DeleteMapping("/role/{roleId}")
	public JupiterResult<Void> deleteByRoleId(@PathVariable("roleId") String roleId) {
		userRoleService.deleteByRoleId(roleId);
		return JupiterResult.success();
	}

	@PostMapping("/assign-roles-to-user")
	public JupiterResult<Void> assignRolesToUser(@RequestBody JupiterUserRoleDTO userRoleDTO) {
		userRoleService.assignRolesToUser(userRoleDTO.getUserUuid(), userRoleDTO.getRoleUuids());
		return JupiterResult.success();
	}

	@PostMapping("/assign-users-to-role/{roleId}")
	public JupiterResult<Void> assignUsersToRole(@PathVariable("roleId") String roleId,
			@RequestBody List<String> userIds) {
		userRoleService.assignUsersToRole(roleId, userIds);
		return JupiterResult.success();
	}
}
